package com.moonshineproject.plugin.svn
{
	import com.moonshineproject.events.SaveFileEvent;
	import com.moonshineproject.plugin.IMenuPlugin;
	import com.moonshineproject.plugin.PluginBase;
	import com.moonshineproject.plugin.menu.vo.MenuItem;
	import com.moonshineproject.plugin.project.event.ProjectEvent;
	import com.moonshineproject.plugin.settings.ISettingsProvider;
	import com.moonshineproject.plugin.settings.vo.ISetting;
	import com.moonshineproject.plugin.settings.vo.PathSetting;
	import com.moonshineproject.plugin.svn.event.SVNEvent;
	import com.moonshineproject.plugin.svn.provider.SubversionProvider;
	import com.moonshineproject.plugin.svn.view.CheckoutDialog;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.filesystem.File;
	
	import mx.core.FlexGlobals;
	import mx.managers.PopUpManager;

	public class SVNPlugin extends PluginBase implements ISettingsProvider, IMenuPlugin
	{
		public static const CHECKOUT_REQUEST:String = "checkoutRequestEvent";
		
		override public function get name():String			{ return "Subversion Plugin"; }
		override public function get author():String		{ return "Moonshine Project Team"; }
		override public function get description():String	{ return "Provides SVN source control features."; }
		
		public var svnBinaryPath:String;
		
		override public function activate():void
		{
			super.activate();
			
			dispatcher.addEventListener(SaveFileEvent.FILE_SAVED, handleFileSave);
			dispatcher.addEventListener(ProjectEvent.ADD_PROJECT, handleProjectOpen);
			dispatcher.addEventListener(CHECKOUT_REQUEST, handleCheckoutRequest);
		}
		
		override public function deactivate():void
		{
			super.deactivate();
			
			dispatcher.removeEventListener(SaveFileEvent.FILE_SAVED, handleFileSave);
			dispatcher.removeEventListener(ProjectEvent.ADD_PROJECT, handleProjectOpen);
			dispatcher.removeEventListener(CHECKOUT_REQUEST, handleCheckoutRequest);
		}
		
		public function getSettingsList():Vector.<ISetting>
		{
			return Vector.<ISetting>([
				new PathSetting(this,'svnBinaryPath', 'SVN Binary', false)
			]);
		}
		
		public function getMenu():MenuItem
		{
			return new MenuItem(
					"Subversion", 
					[
						new MenuItem("Checkout", null, CHECKOUT_REQUEST)
					]
			);
		}
		
		protected function handleFileSave(event:SaveFileEvent):void
		{
			
		}
		
		protected function handleProjectOpen(event:ProjectEvent):void
		{
			// Check if project is versioned with SVN
			if (isVersioned(event.project.folder) == false) return;
			
			// Check if we have a SVN binary
			if (!svnBinaryPath || svnBinaryPath == "") return;
			
			// Create new provider
			var provider:SubversionProvider = new SubversionProvider();
			provider.executable = new File(svnBinaryPath);
			provider.root = event.project.folder;
			
			// Set it to tree data so project view can render it
			event.project.projectFolder.sourceController = provider;
			
			// Load initial data
			provider.refresh(event.project.folder);
		}
		
		protected function handleCheckoutRequest(event:Event):void
		{
			// Check if we have a SVN binary
			if (!svnBinaryPath || svnBinaryPath == "")
			{
				error("No SVN binary set, please check the settings.");
				return;
			}
			
			var checkoutDialog:CheckoutDialog = new CheckoutDialog();
			checkoutDialog.addEventListener('close', handleCheckoutDialogClose);
			checkoutDialog.addEventListener(SVNEvent.EVENT_CHECKOUT, handleCheckout);
			
			PopUpManager.addPopUp(checkoutDialog, FlexGlobals.topLevelApplication as DisplayObject, true);
			PopUpManager.centerPopUp(checkoutDialog);
			checkoutDialog.y -= 20;
		}
		
		protected function handleCheckout(event:SVNEvent):void
		{
			var provider:SubversionProvider = new SubversionProvider();
			provider.executable = new File(svnBinaryPath);
			provider.checkout(event.file, event.url);
			
			handleCheckoutDialogClose(event);
		}
		
		protected function handleCheckoutDialogClose(event:Event):void
		{
			var pop:CheckoutDialog = CheckoutDialog(event.target);
			PopUpManager.removePopUp(pop);
			pop.addEventListener('close', handleCheckoutDialogClose);
			pop.addEventListener(SVNEvent.EVENT_CHECKOUT, handleCheckout);
		}
		
		protected function isVersioned(folder:File):Boolean
		{
			var listing:Array = folder.getDirectoryListing();
			for each (var file:File in listing)
			{
				if (file.name == ".svn")
					return true;
			}
			return false;
		}
	}
}